package com.kjlink.internal.workreport.task.handler;

import cn.xuqiudong.common.base.vo.BooleanWithMsg;
import com.kjlink.internal.workreport.base.enums.ProjectTimeStatsCategory;
import com.kjlink.internal.workreport.helper.ReportCommonHelper;
import com.kjlink.internal.workreport.project.model.Project;
import com.kjlink.internal.workreport.project.service.ProjectService;
import com.kjlink.internal.workreport.report.service.WorkReportService;
import com.kjlink.internal.workreport.stats.model.ProjectTimeStats;
import com.kjlink.internal.workreport.stats.service.ProjectTimeStatsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述:
 * 基于项目的工时统计处理器
 *
 * @author Vic.xu
 * @since 2024-05-27 13:48
 */
@Component
public class ProjectTimeStatsHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectTimeStatsHandler.class);

    @Resource
    private ProjectService projectService;

    @Resource
    private ProjectTimeStatsService projectTimeStatsService;

    @Resource
    private WorkReportService workReportService;


    /**
     * 未关闭的项目列表
     */
    public List<Project> noneClosedProjects() {
        return projectService.noneClosedProjects();
    }

    /**
     * 更新项目总工时
     */
    public BooleanWithMsg updateTotalTime(Project project) {
        try {
            int totalTime = projectService.updateTotalTime(project.getId());
            return BooleanWithMsg.success(ReportCommonHelper.toHoursNumber(totalTime).toPlainString());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BooleanWithMsg.fail("失败:" + e.getMessage());
        }
    }

    /**
     * 更新当月工时
     */
    public BooleanWithMsg updateMonthUsedTime(Project project, int year, int month, int monthDate) {
        try {
            Integer monthTime = workReportService.monthTime(project.getId(), year, month);
            buildProjectTimeStatsAndSave(project, monthDate, ProjectTimeStatsCategory.month, monthTime);
            return BooleanWithMsg.success(ReportCommonHelper.toHoursNumber(monthTime).toPlainString());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BooleanWithMsg.fail("失败:" + e.getMessage());
        }
    }

    public BooleanWithMsg updateYearUsedTime(Project project, int year) {
        try {
            Integer yearTime = workReportService.yearTime(project.getId(), year);
            buildProjectTimeStatsAndSave(project, year, ProjectTimeStatsCategory.year, yearTime);
            return BooleanWithMsg.success(ReportCommonHelper.toHoursNumber(yearTime).toPlainString());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BooleanWithMsg.fail("失败:" + e.getMessage());
        }
    }

    private void buildProjectTimeStatsAndSave(Project project, int date, ProjectTimeStatsCategory category, Integer total) {
        ProjectTimeStats projectTimeStats = new ProjectTimeStats();
        projectTimeStats.setProjectId(project.getId())
                .setCategory(category)
                .setDate(date).setTotal(total);
        ProjectTimeStats entity = projectTimeStatsService.findOne(projectTimeStats);
        if (entity != null) {
            projectTimeStats.setId(entity.getId());
        }
        projectTimeStatsService.save(projectTimeStats);
    }


}
